package com.atguigu.auth.controller;


import com.atguigu.auth.entity.Device;
import com.atguigu.auth.entity.Device;
import com.atguigu.auth.entity.Room;
import com.atguigu.auth.service.DeviceService;
import com.atguigu.auth.service.DeviceService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysUser;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.context.SpringContextResourceAdapter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 设备表，存储用户的设备信息 前端控制器
 * </p>
 *
 * @author universe
 * @since 2024-03-17
 */
@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/auth/device")
public class DeviceController {
    @Autowired
    private DeviceService service;

    @Autowired
    private SysUserService userService;
    //条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        Device device) {
        //创建page对象
        Page<Device> pageParam = new Page<>(page,limit);

        //封装条件，判断条件值不为空
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String Devicename = device.getDeviceName();

        Integer roomId = device.getRoomId();

        String deviceType = device.getDeviceType();
        //判断条件值不为空
        //like 模糊查询
        if(!StringUtils.isEmpty(Devicename)) {
            wrapper.like(Device::getDeviceName,Devicename);
        }

        if (
                roomId !=null
        ){
            wrapper.eq(Device::getRoomId,roomId);
        }
        //ge 大于等于
        if(!StringUtils.isEmpty(deviceType)) {
            wrapper.ge(Device::getDeviceType,deviceType);
        }

        //调用mp的方法实现条件分页查询
        IPage<Device> pageModel = service.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Device device = service.getById(id);
        return Result.ok(device);
    }

    @ApiOperation(value = "根据房间id查询所有设备")
    @GetMapping("getAllByRoomId/{id}")
    public Result getAllByRoomId(@PathVariable Long id) {
        return Result.ok(service.list(new LambdaQueryWrapper<Device>().eq(Device::getRoomId, id)));
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    public Result save(@RequestBody Device device) {
        Principal principal = SecurityContextHolder.getContext().getAuthentication();
        String name = principal.getName();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getName,name);
        SysUser one = userService.getOne(wrapper);
        device.setUserId(Integer.valueOf(one.getId().toString()));
        service.save(device);
        return Result.ok();
    }

    @ApiOperation(value = "更新")
    @PutMapping("update")
    public Result updateById(@RequestBody Device device) {
        service.updateById(device);
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value =  "批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Integer> idList){
        service.removeByIds(idList);
        return Result.ok();
    }
}

